#include <iostream>
#include <cstring>
#include <queue>
#include <map>
#include <vector>
#include <string.h>
using namespace std;

int n;
const int maxn = 1e5 + 1;

int a[maxn];
int b[maxn];

// dp[i][j]表示a的前i位和b的前j位的公共子序列

int main()
{
    cin >> n;
    int dp[n + 10][n + 10];
    memset(dp,0,sizeof(dp));
    for (int i = 1; i <= n; i++)
    {

        cin >> a[i];
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> b[i];
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (a[i] == b[j])
                dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
            else
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
    }
    cout << dp[n][n] << endl;
    system("pause");
    return 0;
}